import { HttpException, HttpExceptionOptions, HttpStatus } from '@nestjs/common'

export class CoreHttpException extends HttpException {
  readonly code: string

  constructor(exception: Error, option?: CoreHttpExceptionOption)
  constructor(message: string | string[], option?: CoreHttpExceptionOption)
  constructor(arg0: Error | string | string[], option?: CoreHttpExceptionOption) {
    const message = typeof arg0 === 'string' ? arg0 : Array.isArray(arg0) ? arg0.join(',') : arg0.message
    const status = option?.status ?? HttpStatus.INTERNAL_SERVER_ERROR

    super(message, status)
    this.message = message
    this.code = option?.code
  }
}

export interface CoreHttpExceptionOption extends HttpExceptionOptions {
  code?: string
  status?: HttpStatus
}
